<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">收货人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="请填写收货人姓名" placeholder-class="placeholder" />
		</view>

		<view class="row b-b">
			<text class="tit">手机号码</text>
			<input class="input" type="number" maxlength="11" v-model="addressData.tel" placeholder="请填写收货人手机号码"
			 placeholder-class="placeholder" />
		</view>

		<view class="row b-b">
			<text class="tit">所在区域</text>
			<input class="input" disabled="disabled" v-model="addressData.addressName" placeholder="请填写收货人地址" placeholder-class="placeholder"
			 @click="showPicker" />
		</view>

		<view class="row b-b">
			<text class="tit">详细地址</text>
			<input class="input" type="text" v-model="addressData.addressDetail" placeholder="街道、楼牌号等" placeholder-class="placeholder" />
		</view>

		<view>
			<view class="row default-row">
				<text class="tit">设置默认地址</text>
				<switch :checked="addressData.isDefault" color="#06AD56" @change="switchChange" />
			</view>
			<view class="rowtit">
				提醒：每次下单时会使用该地址，实际下单地址会根据您购物切换的地址进行判断，请在下单时确认！
			</view>
		</view>

		<button class="add-btn" @click="confirm">保存</button>

		<!--picker-view start-->
		<view class="tui-mask-screen" :class="[showPickerStatus?'tui-mask-show':'']" @tap="hidePicker"></view>
		<view class="tui-picker-box" :class="[showPickerStatus?'tui-pickerbox-show':'']">
			<view class="picker-header tui-list-item">
				<view class="btn-cancle" hover-class="tui-opcity" :hover-stay-time="150" @tap.stop="hidePicker">取消</view>
				<view class="btn-sure" hover-class="tui-opcity" :hover-stay-time="150" @tap.stop="picker">确定</view>
			</view>
			<picker-view indicator-style="height: 50px;" class="picker-view" :value="value" @change="columnPicker">
				<picker-view-column>
					<view v-for="(item,index) in proviceArr" :key="index" class="item">{{item}}</view>
				</picker-view-column>
				<picker-view-column>
					<view v-for="(item,index) in cityArr" :key="index" class="item">{{item}}</view>
				</picker-view-column>
				<picker-view-column>
					<view v-for="(item,index) in districtArr" :key="index" class="item">{{item}}</view>
				</picker-view-column>
			</picker-view>
		</view>
		<!--picker-view end-->

	</view>
</template>

<script>
	const cityData = require('@/utils/picker.city.js')
	import util from "@/utils/util.js"
	export default {
		data() {
			return {
				addressData: {
					name: '',
					tel: '',
					addressName: '',
					province: '',
					city: '',
					county: '',
					addressDetail: '',
					isDefault: false,
				},
				userId: "",
				proviceArr: [],
				cityArr: [],
				districtArr: [],
				value: [0, 0, 0],
				showPickerStatus: false,
				cityList: [],
				delUserId: "", //删除地址的id
			}
		},
		onLoad(option) {
			console.log(option)
			this.userId = option.userId;
			if (option.data) {
				let ids = JSON.parse(option.data)
				this.delUserId = ids.id
				console.log(ids.id)
			}

			let title = '新增收货地址';
			if (option.type === 'edit') {
				title = '编辑收货地址'
				this.addressData = JSON.parse(option.data)
				this.addressData.addressName = this.addressData.province + this.addressData.city + this.addressData.county
			}else{
				this.addressData.isDefault = true
			}

			this.manageType = option.type;

			uni.setNavigationBarTitle({
				title
			})

			this.tui.request('/app/region/list', 'GET', {}, false, false, false, )
				.then(res => {
					if (res.errno == 0) {
						this.cityList = res.data.list
						this.proviceArr = this.toArr(this.cityList);
						this.cityArr = this.toArr(this.cityList[0].children);
						this.districtArr = this.toArr(this.cityList[0].children[0].children)
					}
				}).catch(err => {
					console.log(err)
				})
		},

		//获取导航栏右侧的按钮事件
		onNavigationBarButtonTap(e) {
			uni.showModal({
				content: '确定要删除地址',
				success: (e) => {
					if (e.confirm) {
						this.tui.request('/app/address/delete?userId=' + this.userId, 'POST', {
								id: this.delUserId
							}, false, false, false, )
							.then(res => {

								// uni.reLaunch({
								// 	url: "/pages/address/address"
								// })

								uni.navigateBack({
									delta: 1
								})


							}).catch(err => {
								console.log(err)
							})
					}
				}
			});

		},

		methods: {
			toArr(object) {
				let arr = [];
				for (let i in object) {
					arr.push(object[i].name);
				}
				return arr;
			},

			// 显示picker-view
			showPicker: function() {
				this.showPickerStatus = true
			},

			// 隐藏picker-view
			hidePicker: function() {
				this.showPickerStatus = false
			},

			//picker change切换事件
			columnPicker: function(e) {
				let value = e.detail.value;
				//如果两者下标不一致，表示滚动过
				if (this.value[0] !== value[0]) {
					this.proviceArr = this.proviceArr;
					this.cityArr = this.toArr(this.cityList[value[0]].children);
					this.districtArr = this.toArr(this.cityList[value[0]].children[0].children);
					this.value = [value[0], 0, 0]
				} else if (this.value[1] !== value[1]) {
					this.proviceArr = this.proviceArr;
					this.cityArr = this.cityArr;
					this.districtArr = this.toArr(this.cityList[value[0]].children[value[1]].children);
					this.value = [value[0], value[1], 0]
				} else {
					this.value = value
				}
			},

			//确定按钮
			picker: function(e) {
				let value = this.value;
				if (cityData.length > 0) {
					let provice = this.cityList[value[0]].name;
					let city = this.cityList[value[0]].children[value[1]].name;
					let district = this.cityList[value[0]].children[value[1]].children[value[2]].name;
					this.showPickerStatus = false
					this.addressData.province = provice;
					this.addressData.city = city;
					this.addressData.county = district;
					this.addressData.addressName = provice + city + district;
					console.log("addressData", this.addressData.addressName)
				}
			},

			//设置默认地址
			switchChange: function(e) {
				this.addressData.isDefault = e.detail.value;
			},

			//提交
			confirm: function(e) {
				let self = this;
				let data = this.addressData;
				if (!data.name) {
					this.$api.msg('请填写收货人姓名');
					return;
				}else if (util.isNullOrEmpty(data.tel)) {
					_this.tui.toast('请输入手机号码');
					return
				}else if (!util.isMobile(data.tel)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}else if (!data.addressName) {
					this.$api.msg('请在选择所在位置');
					return;
				}else if (!data.addressDetail) {
					this.$api.msg('请填写门牌号信息');
					return;
				}

				this.tui.request('/app/address/save?userId=' + this.userId, 'POST', {
							id: data.id,
							name: data.name,
							tel: data.tel,
							province: data.province,
							city: data.city,
							county: data.county,
							addressDetail: data.addressDetail,
							isDefault: data.isDefault
						},
						false, false, false, )
					.then(res => {
						if (res.errno == 0) {
							this.$api.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
							uni.navigateBack()

						} else {
							console.log(res)
							this.tui.toast(res.errmsg)
						}
					}).catch(err => {
						console.log(err)
					})
					
				this.$api.prePage().refreshList(data, this.manageType);
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 25%;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.rowtit {
		font-size: 12px;
		font-family: PingFang SC;
		font-weight: 400;
		color: rgba(153, 153, 153, 1);
		line-height: 12px;
		padding: 0 30upx;
		background: #ffffff;
		padding-bottom: 12px;
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 120px auto;
		font-size: $font-lg;
		color: #fff;
		background-color: #D14798;
		border-radius: 45px;
		box-shadow: 1px 2px 5px rgba(110, 96, 158, 0.4);
	}



	/* picker start*/

	.tui-mask-screen {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		z-index: 99996;
		transition: all 0.3s ease-in-out;
		opacity: 0;
		visibility: hidden;
	}

	.tui-mask-show {
		opacity: 1;
		visibility: visible;
	}

	.tui-picker-box {
		width: 100%;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 99999;
		visibility: hidden;
		transform: translate3d(0, 100%, 0);
		transform-origin: center;
		transition: all 0.3s ease-in-out;
		min-height: 20rpx;
		background: #fff;
	}

	.tui-pickerbox-show {
		transform: translate3d(0, 0, 0);
		visibility: visible;
	}

	.picker-header {
		width: 100%;
		height: 90rpx;
		padding: 0 46rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		font-size: 32rpx;
		background: #fff;
	}

	.tui-list-item::after {
		left: 0;
	}

	.btn-cancle {
		padding: 20rpx;
		color: #888;
	}

	.btn-sure {
		padding: 20rpx;
		color: #5677fc;
	}

	.picker-view {
		width: 100%;
		height: 260px;
	}

	.item {
		line-height: 50px;
		text-align: center;
	}

	/* picker end*/

	.search-box {
		width: 672rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
		margin: 80rpx auto 40rpx auto;
		position: relative;
		/* #ifdef MP-ALIPAY */
		background: #fff;
		/* #endif */
	}

	.search-box::after {
		content: '';
		position: absolute;
		height: 200%;
		width: 200%;
		border: 1rpx solid #dbe1ef;
		transform-origin: 0 0;
		-webkit-transform-origin: 0 0;
		-webkit-transform: scale(0.5);
		transform: scale(0.5);
		left: 0;
		top: 0;
		border-radius: 20rpx;
		pointer-events: none;
	}

	.s-input {
		height: 86rpx;
		padding: 0 21rpx;
		display: flex;
		align-items: center;
	}

	.s-img {
		width: 34rpx;
		height: 34rpx;
		margin-right: 17rpx;
		flex-shrink: 0;
	}

	.input {
		font-size: 32rpx;
		color: #353535;
		width: 565rpx;
		padding-right: 5rpx;
		box-sizing: border-box;
		// height: 100%;
	}

	.img30 {
		height: 30rpx;
		width: 30rpx;
	}

	.s-select {
		position: relative;
		z-index: 9;
	}

	.s-select::before {
		content: '';
		position: absolute;
		border-top: 1rpx solid #dbe1ef;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		top: 0;
		right: 0;
		left: 0;
	}

	.text {
		color: #353535;
		font-size: 32rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.wid27 {
		width: 27%;
	}

	.wid46 {
		width: 46%;
	}

	.img32 {
		height: 32rpx;
		width: 32rpx;
	}

	.pdr20 {
		padding-right: 20rpx;
	}

	.flr {
		margin-left: auto;
	}

	.btn-select {
		padding: 20rpx 40rpx;
	}
</style>
